-- UIAirShipDesc
-- Create by chendh
-- 飞艇帮助界面

--UIWorkshopDesc继承自Layer
UIAirShipDesc = class("UIAirShipDesc", function()
    return cc.Layer:create();
end);

function UIAirShipDesc.create()
    return UIAirShipDesc.new();
end

local MATERIAL_STRAT_ID = 2;
local MATERIAL_END_ID = 9;

--构造函数
function UIAirShipDesc:ctor()
    self:setName("UIAirShipDesc");

    --初始化
    local node = cc.CSLoader:createNode("layout/sky_city/AirShipDesc.csb");
    self:addChild(node);
    self.node = node;

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize();
end

-- 重绘
function UIAirShipDesc:redraw()
    -- 标题
    local combatTitleLabel = findChildByName(self.node,"CT/combat_title");
    local combatDescLabel = findChildByName(self.node,"CT/combat_desc");
    local selectTitleLabel = findChildByName(self.node,"CT/select_title");
    local selectDescLabel = findChildByName(self.node,"CT/select_desc");

    TextStyleM.setTextStyle(combatTitleLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    TextStyleM.setTextStyle(selectTitleLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    TextStyleM.setTextStyle(combatDescLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    TextStyleM.setTextStyle(selectDescLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    combatTitleLabel = applyString(combatTitleLabel, getLocStr("airship_combat_title"));
    combatDescLabel = applyString(combatDescLabel, getLocStr("airship_combat_desc"));
    selectTitleLabel = applyString(selectTitleLabel, getLocStr("airship_select_title"));
    selectDescLabel = applyString(selectDescLabel, getLocStr("airship_select_desc"));

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,
        { combatTitleLabel, combatDescLabel, selectTitleLabel, selectDescLabel },
        { AlignM.DEFAULT_GAP, AlignM.DEFAULT_GAP * 2, AlignM.DEFAULT_GAP, AlignM.DEFAULT_GAP });
end

-- 注册点击事件
function UIAirShipDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("ship_return");
            UIMgr.getCurrentScene():removeFormByName("UIAirShipDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册点击任意地方关闭当前窗口事件
    local btnBgBack = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIAirShipDesc");
        end
    end
    btnBgBack:addTouchEventListener(onBgBackClick);
end

-- 适配
function UIAirShipDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end


